﻿var pageSize = 20;
window.onload = function (){
	init_grid();
	init_page(2);
}

function init_page(cYdzt){
	var gcCount = new AjaxProxy();
	gcCount.addParm("pageSize",pageSize);
	gcCount.addParm("cYdzt",cYdzt);
	gcCount.invoke("message.getSumPages",false);
	var flag = gcCount.getString("PN_RET");
	if(flag!=0){
		alert(gcCount.getString("PC_MSG"));
		return;
	}
	var pages = gcCount.getString("totalPages");
	if(pages == 0){
		$('#codeTable').datagrid("loadData",{rows:{}});
	}
	pagingRegister(pages,function(curr){
		//$('#audits').linkbutton('disable');
		var gcRows = new AjaxProxy();
		gcRows.addParm("currPage",curr);
		gcRows.addParm("pageRows",pageSize);
		gcRows.addParm("cYdzt",cYdzt);
		gcRows.invoke("message.getPageRows",false);
		var ctData = {"rows":gcRows.getData("P_RESULT")};
		if(!ctData.rows){
			$('#codeTable').datagrid("loadData",{rows:{}});
		}
		$('#codeTable').datagrid("loadData",ctData);
	});
}

function init_grid(){
	$('#codeTable').datagrid({
		height:600,
		singleSelect:false,
		rownumbers : true,
		onSelect:function(index,data){
			//getSel(index,data);
		},
		onClickRow:function(index,data){
			getSel(index,data);
		},
		frozenColumns:[[
	        {field:'ck',checkbox:true}
		]],
		columns:[[
			{field:'cYdzt',title:'<img src="../../../images/email.gif" />',width:30,align:'center',
			formatter:function(value,rec){
				if(value==0){
					return '<img src="../../../images/email.gif" />';
				} else {
					return '<img src="../../../images/email0.gif" />';
				}
			}},
			{field:'nSendid',title:'发件人',width:80,align:'center',
			formatter:function(value,rec){
				if(value=="-1")
					return "<font color=red>系统管理员</font>";
				else
					return "<font color=green>其它</font>";
			}},
			{field:'cTitle',title:'主题',width:410,align:'center',
			formatter:function(value,rec){
				if(rec.cYdzt==0){
					return "<div align='left' style='text-overflow:ellipsis;overflow:hidden;white-space: nowrap; color:#000000'><b>"+value+"</b><font color=#a0a0a0>&nbsp;-&nbsp;"+rec.cContent+"</font></div>";
				}else{
					return "<div align='left' style='text-overflow:ellipsis;overflow:hidden;white-space: nowrap; color:#000000'>"+value+"<font color=#a0a0a0>&nbsp;-&nbsp;"+rec.cContent+"</font></div>";
				}
			}},
			{field:'dFssj',title:'时间',width:140,align:'center',
			formatter:function(value,rec){
				if(rec.cYdzt==0){
					return "<font color=#000000><b>"+value+"</b></font>";
				}else{
					return "<font color=#000000>"+value+"</font>";
				}
			}}
		]],
		toolbar:[{
			text:'写信',
			id:"btnInsert",
			iconCls:'icon-edit',
			handler:function(){
				setTimeout(function(){
					window.location.replace("/filter/user/newmsg?cType=1&cContent=");
				},10);
			}
		}/*,'-',{
			text:'回复',
			id:"btnUpd",
			disabled:true,
			iconCls:'icon-undo',
			handler:function(){
				f_insert($('#codeTable').datagrid("getSelected",null));
			}
		}*/,'-',{
			text:'删除',
			id:"btnDel",
			disabled:false,
			iconCls:'icon-remove',
			handler:function(){
				var rows = $('#codeTable').datagrid('getSelections');
				if(!rows||(rows.length==0)){
					alert("提示：请选择要删除的消息!");
					return;
				}
				if(!confirm("提示：你确定要删除当前选中的消息吗？"))return;
				var saveFlag = true;
				for(var i = 0 ; i < rows.length ; i ++) {
					var gcDel = new AjaxProxy();
					gcDel.addParm("nId",rows[i].nId);
					gcDel.invoke("message.deleteUserMessage",false);
					var flag = gcDel.getString("PN_RET");
					if(flag != 0) {
						alert(gcDel.getString("PC_MSG"));
						saveFlag = false;
						break;
					}
				}
				
				if(saveFlag) {
					alert("消息删除成功!");
				}
				
				init_page(2);
			}
		},'-',{
			text:'未阅信件',
			disabled:false,
			id:"btnQd",
			iconCls:'icon-ok',
			handler:function(){
				init_page(0);
			}
		},'-',{
			text:'全部信件',
			id:"btnJs",
			disabled:false,
			iconCls:'icon-undo',
			handler:function(){
				init_page(2);
			}
		}]
	});
}

function getSel(index,data){
	//alert(data.nId);
	window.location.replace("/filter/user/messageDetail?nId="+data.nId+"&cYdzt="+data.cYdzt+"&cYxhf="+data.cYxhf+"&cZt="+data.cZt);
}